[[...path]].page.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import React from 'react';
  2. import { IUserHasId, IPagePopulatedToShowRevision } from '@growi/core';
  3. import {
  4. GetServerSideProps, GetServerSidePropsContext,
  5. } from 'next';
  6. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  7. import dynamic from 'next/dynamic';
  8. import Head from 'next/head';
  9. import superjson from 'superjson';
  10. import { useCurrentGrowiLayoutFluidClassName } from '~/client/services/layout';
  11. import { MainPane } from '~/components/Layout/MainPane';
  12. import { ShareLinkLayout } from '~/components/Layout/ShareLinkLayout';
  13. import { GrowiContextualSubNavigationSubstance } from '~/components/Navbar/GrowiContextualSubNavigation';
  14. import { Page } from '~/components/Page';
  15. import type { PageSideContentsProps } from '~/components/PageSideContents';
  16. import { DrawioViewerScript } from '~/components/Script/DrawioViewerScript';
  17. import { SupportedAction, SupportedActionType } from '~/interfaces/activity';
  18. import { CrowiRequest } from '~/interfaces/crowi-request';
  19. import { RendererConfig } from '~/interfaces/services/renderer';
  20. import { IShareLinkHasId } from '~/interfaces/share-link';
  21. import type { PageDocument } from '~/server/models/page';
  22. import {
  23. useCurrentUser, useCurrentPathname, useCurrentPageId, useRendererConfig, useIsSearchPage,
  24. useShareLinkId, useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsSearchScopeChildrenAsDefault, useDrawioUri, useIsContainerFluid,
  25. } from '~/stores/context';
  26. import loggerFactory from '~/utils/logger';
  27. import { NextPageWithLayout } from '../_app.page';
  28. import {
  29. CommonProps, getServerSideCommonProps, generateCustomTitle, getNextI18NextConfig,
  30. } from '../utils/commons';
  31. const logger = loggerFactory('growi:next-page:share');
  32. const PageSideContents = dynamic<PageSideContentsProps>(() => import('~/components/PageSideContents').then(mod => mod.PageSideContents), { ssr: false });
  33. // const Comments = dynamic(() => import('~/components/Comments').then(mod => mod.Comments), { ssr: false });
  34. const ShareLinkAlert = dynamic(() => import('~/components/Page/ShareLinkAlert'), { ssr: false });
  35. const ForbiddenPage = dynamic(() => import('~/components/ForbiddenPage'), { ssr: false });
  36. type Props = CommonProps & {
  37. shareLinkRelatedPage?: IShareLinkRelatedPage,
  38. shareLink?: IShareLinkHasId,
  39. isExpired: boolean,
  40. disableLinkSharing: boolean,
  41. isSearchServiceConfigured: boolean,
  42. isSearchServiceReachable: boolean,
  43. isSearchScopeChildrenAsDefault: boolean,
  44. drawioUri: string | null,
  45. rendererConfig: RendererConfig,
  46. };
  47. type IShareLinkRelatedPage = IPagePopulatedToShowRevision & PageDocument;
  48. superjson.registerCustom<IShareLinkRelatedPage, string>(
  49. {
  50. isApplicable: (v): v is IShareLinkRelatedPage => {
  51. return v != null
  52. && v.toObject != null
  53. && v.lastUpdateUser != null
  54. && v.creator != null
  55. && v.revision != null;
  56. },
  57. serialize: (v) => { return superjson.stringify(v.toObject()) },
  58. deserialize: (v) => { return superjson.parse(v) },
  59. },
  60. 'IShareLinkRelatedPageTransformer',
  61. );
  62. // GrowiContextualSubNavigation for shared page
  63. // get page info from props not to send request 'GET /page' from client
  64. type GrowiContextualSubNavigationForSharedPageProps = {
  65. currentPage?: IPagePopulatedToShowRevision,
  66. isLinkSharingDisabled: boolean,
  67. }
  68. const GrowiContextualSubNavigationForSharedPage = (props: GrowiContextualSubNavigationForSharedPageProps): JSX.Element => {
  69. const { currentPage, isLinkSharingDisabled } = props;
  70. if (currentPage == null) { return <></> }
  71. return <GrowiContextualSubNavigationSubstance currentPage={currentPage} isLinkSharingDisabled={isLinkSharingDisabled}/>;
  72. };
  73. const SharedPage: NextPageWithLayout<Props> = (props: Props) => {
  74. useIsSearchPage(false);
  75. useShareLinkId(props.shareLink?._id);
  76. useCurrentPageId(props.shareLink?.relatedPage._id);
  77. useCurrentUser(props.currentUser);
  78. useCurrentPathname(props.currentPathname);
  79. useRendererConfig(props.rendererConfig);
  80. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  81. useIsSearchServiceReachable(props.isSearchServiceReachable);
  82. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  83. useDrawioUri(props.drawioUri);
  84. useIsContainerFluid(props.isContainerFluid);
  85. const growiLayoutFluidClass = useCurrentGrowiLayoutFluidClassName();
  86. const isNotFound = props.shareLink == null || props.shareLink.relatedPage == null || props.shareLink.relatedPage.isEmpty;
  87. const isShowSharedPage = !props.disableLinkSharing && !isNotFound && !props.isExpired;
  88. const shareLink = props.shareLink;
  89. const title = generateCustomTitle(props, 'GROWI');
  90. const sideContents = shareLink != null
  91. ? <PageSideContents page={shareLink.relatedPage} />
  92. : <></>;
  93. // const footerContents = shareLink != null && isPopulated(shareLink.relatedPage.revision)
  94. // ? (
  95. // <>
  96. // <Comments pageId={shareLink._id} pagePath={shareLink.relatedPage.path} revision={shareLink.relatedPage.revision} />
  97. // </>
  98. // )
  99. // : <></>;
  100. return (
  101. <>
  102. <Head>
  103. <title>{title}</title>
  104. </Head>
  105. <div className={`dynamic-layout-root ${growiLayoutFluidClass} h-100 d-flex flex-column justify-content-between`}>
  106. <header className="py-0 position-relative">
  107. {isShowSharedPage
  108. && <GrowiContextualSubNavigationForSharedPage currentPage={props.shareLinkRelatedPage} isLinkSharingDisabled={props.disableLinkSharing} />}
  109. </header>
  110. <div id="grw-fav-sticky-trigger" className="sticky-top"></div>
  111. <MainPane
  112. sideContents={sideContents}
  113. // footerContents={footerContents}
  114. >
  115. { props.disableLinkSharing && (
  116. <div className="mt-4">
  117. <ForbiddenPage isLinkSharingDisabled={props.disableLinkSharing} />
  118. </div>
  119. )}
  120. { (isNotFound && !props.disableLinkSharing) && (
  121. <div className="container-lg">
  122. <h2 className="text-muted mt-4">
  123. <i className="icon-ban" aria-hidden="true" />
  124. <span> Page is not found</span>
  125. </h2>
  126. </div>
  127. )}
  128. { (props.isExpired && !props.disableLinkSharing && shareLink != null) && (
  129. <div className="container-lg">
  130. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  131. <h2 className="text-muted mt-4">
  132. <i className="icon-ban" aria-hidden="true" />
  133. <span> Page is expired</span>
  134. </h2>
  135. </div>
  136. )}
  137. {(isShowSharedPage && shareLink != null) && (
  138. <>
  139. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  140. <Page currentPage={props.shareLinkRelatedPage} />
  141. </>
  142. )}
  143. </MainPane>
  144. </div>
  145. </>
  146. );
  147. };
  148. SharedPage.getLayout = function getLayout(page) {
  149. return (
  150. <>
  151. <DrawioViewerScript />
  152. <ShareLinkLayout>{page}</ShareLinkLayout>
  153. </>
  154. );
  155. };
  156. function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): void {
  157. const req: CrowiRequest = context.req as CrowiRequest;
  158. const { crowi } = req;
  159. const { configManager, searchService, xssService } = crowi;
  160. props.disableLinkSharing = configManager.getConfig('crowi', 'security:disableLinkSharing');
  161. props.isSearchServiceConfigured = searchService.isConfigured;
  162. props.isSearchServiceReachable = searchService.isReachable;
  163. props.isSearchScopeChildrenAsDefault = configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  164. props.drawioUri = configManager.getConfig('crowi', 'app:drawioUri');
  165. props.rendererConfig = {
  166. isEnabledLinebreaks: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  167. isEnabledLinebreaksInComments: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  168. adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
  169. isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),
  170. plantumlUri: process.env.PLANTUML_URI ?? null,
  171. blockdiagUri: process.env.BLOCKDIAG_URI ?? null,
  172. // XSS Options
  173. isEnabledXssPrevention: configManager.getConfig('markdown', 'markdown:rehypeSanitize:isEnabledPrevention'),
  174. attrWhiteList: xssService.getAttrWhiteList(),
  175. tagWhiteList: xssService.getTagWhiteList(),
  176. highlightJsStyleBorder: configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  177. };
  178. }
  179. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  180. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  181. props._nextI18Next = nextI18NextConfig._nextI18Next;
  182. }
  183. function getAction(props: Props): SupportedActionType {
  184. let action: SupportedActionType;
  185. if (props.isExpired) {
  186. action = SupportedAction.ACTION_SHARE_LINK_EXPIRED_PAGE_VIEW;
  187. }
  188. else if (props.shareLink == null) {
  189. action = SupportedAction.ACTION_SHARE_LINK_NOT_FOUND;
  190. }
  191. else {
  192. action = SupportedAction.ACTION_SHARE_LINK_PAGE_VIEW;
  193. }
  194. return action;
  195. }
  196. async function addActivity(context: GetServerSidePropsContext, action: SupportedActionType): Promise<void> {
  197. const req: CrowiRequest = context.req as CrowiRequest;
  198. const parameters = {
  199. ip: req.ip,
  200. endpoint: req.originalUrl,
  201. action,
  202. user: req.user?._id,
  203. snapshot: {
  204. username: req.user?.username,
  205. },
  206. };
  207. await req.crowi.activityService.createActivity(parameters);
  208. }
  209. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  210. const req = context.req as CrowiRequest<IUserHasId & any>;
  211. const { crowi, params } = req;
  212. const result = await getServerSideCommonProps(context);
  213. if (!('props' in result)) {
  214. throw new Error('invalid getSSP result');
  215. }
  216. const props: Props = result.props as Props;
  217. try {
  218. const ShareLinkModel = crowi.model('ShareLink');
  219. const shareLink = await ShareLinkModel.findOne({ _id: params.linkId }).populate('relatedPage');
  220. if (shareLink != null) {
  221. props.shareLinkRelatedPage = await shareLink.relatedPage.populateDataToShowRevision();
  222. props.isExpired = shareLink.isExpired();
  223. props.shareLink = shareLink.toObject();
  224. }
  225. }
  226. catch (err) {
  227. logger.error(err);
  228. }
  229. injectServerConfigurations(context, props);
  230. await injectNextI18NextConfigurations(context, props);
  231. await addActivity(context, getAction(props));
  232. return {
  233. props,
  234. };
  235. };
  236. export default SharedPage;